<?php
require_once dirname(__FILE__) . '/config.php';
require_once 'Net/NNTP.php';
require_once 'MDB2.php';
require_once 'Mail/mimeDecode.php';
require_once 'Console/ProgressBar.php';

date_default_timezone_set('UTC');


class PEAR_QA_Activitytracker
{
    protected $db = null;

    public function __construct()
    {
        $this->db = MDB2::connect($GLOBALS['activitytracker']['dsn']);
        $this->db->loadModule('Extended', null, false);
    }



    public function getLastMessageNumber($strGroup)
    {
        $sql = 'SELECT MAX(post_server_number) FROM posts'
            . ' WHERE post_group = ' . $this->db->quote($strGroup)
            . ' GROUP BY post_group';
        $nNumber = $this->db->getOne($sql);

        if (PEAR::isError($nNumber)) {
            echo $nNumber->getMessage() . "\n" . $nNumber->getUserInfo() . "\n";
            die();
        }
        return $nNumber;
    }



    public function addPost($strGroup, $strFrom, $strDate, $strSubject, $nNumber)
    {
        $nDate = strtotime($strDate);
        if ($nDate == 0) {
            //invalid date
            return false;
        }
        if ($strSubject === '') {
            $strSubject = 'no subject';
        }

        $strSqlDate = date('Y-m-d H:i:s', $nDate);
        $sql = 'INSERT INTO posts (post_group, post_date, post_from'
            . ', post_subject, post_server_number) VALUES('
            . $this->db->quote($strGroup)
            . ', ' . $this->db->quote($strSqlDate)
            . ', ' . $this->db->quote($strFrom)
            . ', ' . $this->db->quote($strSubject)
            . ', ' . $this->db->quote($nNumber)
            . ')';
//echo $sql . "\n";
        $ret = $this->db->query($sql);
        if (PEAR::isError($ret)) {
            echo $ret->getMessage() . "\n" . $ret->getUserInfo() . "\n";
            die();
        }
        return true;
    }
}


function Mail_mimeDecode_mergeHeaders($arHeaders)
{
    $headers = array();
    foreach ($arHeaders as $value) {
        if (isset($headers[strtolower($value['name'])]) AND !is_array($headers[strtolower($value['name'])])) {
            $headers[strtolower($value['name'])]   = array($headers[strtolower($value['name'])]);
            $headers[strtolower($value['name'])][] = $value['value'];

        } elseif (isset($return->headers[strtolower($value['name'])])) {
            $headers[strtolower($value['name'])][] = $value['value'];

        } else {
            $headers[strtolower($value['name'])] = $value['value'];
        }
    }
    return $headers;
}


$at   = new PEAR_QA_Activitytracker();
$nntp = new Net_NNTP();
$ret  = $nntp->connect($GLOBALS['activitytracker']['news']['server']);
if (PEAR::isError($ret)) {
    die('Error connecting to NNTP: ' . $ret->getMessage() . "\n" . $ret->getUserInfo() . "\n");
}

$arMailMimeDecodeParams = array(
    'include_bodies' => false,
    'decode_bodies'  => false,
    'decode_headers' => true
);

foreach ($GLOBALS['activitytracker']['news']['groups'] as $strGroup) {
    $arData = $nntp->selectGroup($strGroup);
    if (PEAR::isError($arData)) {
        die($arData->getMessage() . "\n" . $arData->getUserInfo() . "\n");
    }
    $nLast  = $arData['last'];
    $nFirst = $arData['first'];

    $nLastInDb = $at->getLastMessageNumber($strGroup);
    $nMessages = $nLast - $nFirst;

    echo $strGroup . ': ' . $nMessages . " messages\n";
    if ($nLastInDb !== null && $nLastInDb >= $nLast) {
        echo ' All current, skipping group' . "\n";
        continue;
    }


    $nFirstToFetch = $nLastInDb === null ? $nFirst : $nLastInDb + 1;
    echo ' Fetching from ' . $nFirstToFetch . ' to ' . $nLast . "\n";

    $nMessagesToFetch = $nLast - $nFirstToFetch;

    $bar = new Console_ProgressBar(
        '- %fraction% [%bar%] %percent% ETA: %estimate%',
        '=>',
        '-',
        78,
        $nMessagesToFetch
    );

    $decoder = new Mail_mimeDecode("a: b\n\nasd");

    $nPageSize = 99;
    $nNext     = $nFirstToFetch;
    $nPos      = -1;
    do {
        $nPageEnd = $nNext + $nPageSize;
        if ($nPageEnd > $nLast) {
            $nPageEnd = $nLast;
        }
        $arHeaders = $nntp->getOverview($nNext, $nPageEnd);
        if (PEAR::isError($arHeaders)) {
            break;
        }

        $nId       = $nNext - 1;
        foreach ($arHeaders as $arHeader) {
            ++$nId;
            ++$nPos;
            if (PEAR::isError($arHeader)) {
                //article not found?
                echo ' Skipping '  . $nId . ' because of error' . "\n";
                continue;
            }
/*
            $decoder->_decode_headers = true;
            $arHeader = Mail_mimeDecode_mergeHeaders(
                $decoder->_parseHeaders($strHeader)
            );
*/
//var_dump($arHeader);
            $strSubject = utf8_encode($decoder->_decodeHeader($arHeader['Subject']));
            $strDate    = $arHeader['Date'];
            $strSender  = utf8_encode($decoder->_decodeHeader($arHeader['From']));
            //var_dump($strSubject, $strDate, $strSender, $nId);
            //die();
            $at->addPost($strGroup, $strSender, $strDate, $strSubject, $nId);
            $bar->update($nPos);
        }
        $nNext = $nPageEnd + 1;
    } while ($nNext < $nLast);
/*
    $nPos = 0;
    for ($nId = $nFirstToFetch; $nId <= $nLast; $nId++) {
        $strHeaders = $nntp->getHeaderRaw($nId);
        if (PEAR::isError($strHeaders)) {
            //article not found?
            continue;
        }

        $decoder = new Mail_mimeDecode($strHeaders . "\n\nasd");
        $decoder->_decode_headers = true;
        $arHeaders = Mail_mimeDecode_mergeHeaders(
            $decoder->_parseHeaders($strHeaders)
        );
/*
        $decoded = $decoder->decode($arMailMimeDecodeParams);
        if (PEAR::isError($decoded)) {
            echo $decoded->getMessage() . "\n" . $decoded->getUserInfo() . "\n";
            die();
        }
        $arHeaders = $decoded->headers;
*//*
        //$arHeaders  = $nntp->splitHeaders($nId);
        $strSubject = utf8_encode($arHeaders['subject']);
        $strDate    = $arHeaders['date'];
        $strSender  = utf8_encode($arHeaders['from']);
        //var_dump($strSubject, $strDate, $strSender);
        $at->addPost($strGroup, $strSender, $strDate, $strSubject, $nId);
        $bar->update(++$nPos);
    }*/
    echo "\n";
/**/
}

$nntp->quit();
?>